package com.bestcem.xm.deliver.mq.receiver.biz;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.deliver.mq.dto.*;
import com.bestcem.xm.deliver.service.DeliverMessageReceiverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ReceiverService {
    private final DeliverMessageReceiverService deliverMessageReceiverService;

    public ReceiverService(DeliverMessageReceiverService deliverMessageReceiverService) {
        this.deliverMessageReceiverService = deliverMessageReceiverService;
    }
    /*private DeliverMessageReceiverService deliverMessageReceiverService;

    @Autowired
    public void setDeliverMessageReceiverService(DeliverMessageReceiverService deliverMessageReceiverService) {
        this.deliverMessageReceiverService = deliverMessageReceiverService;
    }*/

    public void wechatmpDeliver(GenericMessage<WechatmpDeliverMessageDto> msg) {
        deliverMessageReceiverService.executeDeliverWechatmpAsync(msg.getPayload());
    }

    public void emailDeliver(GenericMessage<EmailDeliverMessageDto> msg) {
        deliverMessageReceiverService.executeDeliverEmailAsync(msg.getPayload());
    }

    public void smsDeliver(GenericMessage<SmsDeliverMessageDto> msg) {
        deliverMessageReceiverService.executeDeliverSmsAsync(msg.getPayload());
    }

    public void saveCustomDeliverLink(GenericMessage<CustomDeliverLinkMessageDto> msg) {
        deliverMessageReceiverService.executeSaveCustomDeliverLink(msg.getPayload());
    }

    public void qdesProjectCreated(GenericMessage<CreateProjectDto> msg) {
        deliverMessageReceiverService.qdesProjectCreated(msg.getPayload());
    }

    public void projectDeleted(GenericMessage<DeleteProjectDto> msg) {
        deliverMessageReceiverService.changeDeliverStatuOnQdesDelete(msg.getPayload());
        // 后续的二次投放也要删除
        deliverMessageReceiverService.autoDelNoDisturbProject(msg.getPayload());
        // 私有化不扣费 deliverMessageReceiverService.deletePreChargesTask(msg);
    }

    public void projectQuestionDelete(GenericMessage<JSONObject> msg) {
        //二次投放删除，目前没有二次投放，没写
    }

    //@mq.subscribe(topics.survey_respondent_unfinish)
    //@mq.subscribe(topics.survey_respondent_submit)
    //async def deliver_survey_respondent_submit(data):
    //公用的
    public void deliverSurveyRespondentSubmit(GenericMessage<JSONObject> msg) {
        try {
            deliverMessageReceiverService.deliverSurveyRespondentSubmit(msg.getPayload());
        } catch (Exception e) {
            log.error("deliverSurveyRespondentSubmit error", e);
        }
    }


    /**
     * 失效的节点恢复
     */
    public void defaultDeliverStoreInvalidate(GenericMessage<JSONObject> msg) {
        deliverMessageReceiverService.defaultDeliverStoreInvalidate(msg.getPayload());
    }

    /**
     * 当用户层级title改变时，SurveyRegion中和DefaultDeliver 中的组织名称（title）需要修改
     */
    public void userGroupTitleModifyList(GenericMessage<JSONObject> msg) {
        deliverMessageReceiverService.userGroupTitleModifyList(msg.getPayload());
    }

    /**
     * 当用户层级改变, 新增门店时，分发创建对应的默认投放
     */
    public void userNewStoreList(GenericMessage<JSONObject> msg) {
        deliverMessageReceiverService.userNewStoreList(msg.getPayload());
    }

    /**
     * 失效的节点
     */
    public void userGroupInvalidate(GenericMessage<JSONObject> msg) {
        deliverMessageReceiverService.userGroupInvalidate(msg.getPayload());
    }

    public void extparamUpdate(GenericMessage<OpenplatformExtParamUpdateDto> msg) {
        deliverMessageReceiverService.extparamUpdate(msg.getPayload());
    }

    public void deliverSurveyExtVersionUpdate(GenericMessage<OpenplatformExtParamUpdateDto> msg) {
        deliverMessageReceiverService.deliverSurveyExtVersionUpdate(msg.getPayload());
    }

    public void surveyRespondentSync(GenericMessage<JSONObject> msg) {
        deliverMessageReceiverService.surveyRespondentSync(msg.getPayload());
    }
}
